#pragma once

#include "TypeTraits.hh"

#include <new>

namespace snow
{

template <class T1, class T2>
inline void construct(T1 *p1, const T2 &value)
{
    new (p1) T1(value);// 调用T1的构造函数 T1::T1(value)
}

template <class T>
inline void destroy(T *p)
{
    p->~T(); // 显示调用析构函数
}

// is POD
// if value_type has trivial destructor
template <class ForwardIterator>
inline void _destroy(ForwardIterator first, ForwardIterator last, _true_type)
{
}

// is not POD
// if value_type has non-trivial destructor
// 有复杂构造函数，显示调用析构
template <class ForwardIterator>
inline void _destroy(ForwardIterator first, ForwardIterator last, _false_type)
{
    for (; first != last; ++first) {
        destroy(&*first);
    }
}

// 泛化版本
template <class ForwardIterator>
inline void destroy(ForwardIterator first, ForwardIterator last)
{
    typedef typename _type_traits<ForwardIterator>::is_POD_type is_POD_type;
    _destroy(first, last, is_POD_type());
}

} // namespace snow